﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using Newtonsoft.Json;
using System.IO;

namespace AUV5.Web.Service
{
    public partial class ServicePagedList : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /// <param name="category">服务类别(ID或enName),可以为空</param>
            /// <param name="order">排序类别(Random,Newest,Popularity,Hotsale,Rating)</param>
            /// <param name="username">用户名（可以为空）</param>
            /// <param name="mode">显示模式（1：Show,2:Manage,3:All）</param>
            /// <param name="pageindex">当前页，不能为空</param>
            /// <param name="pagesize">每页显示记录数，不能为空</param>
            /// <param name="recordcount">记录总数，不能为空</param>

            string category = Request["category"] ?? "";
            int orderindex = 0;
            
            string userid = Request["userid"] ?? "";
            string key = Request["key"] ?? "";
            int mode = 1;
            int hasvideo = 0;
            int pageindex = 0;
            int pagesize = 0;
            try
            {
                orderindex = int.Parse(Request["order"] ?? "2");
                hasvideo = int.Parse(Request["hasvideo"] ?? "0");
                pageindex = int.Parse(Request["pageindex"]??"0");
                pagesize = int.Parse(Request["pagesize"]??"0");
            }
            catch
            {
                Response.Write("113");//无效的整型参数
                return;
            }

            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, category, orderindex, userid, key, mode, hasvideo, pageindex, pagesize))
            {
                Response.Write("112");//密钥不正确
                return;
            }

            if (Request["pagesize"] != null && Request["pageindex"] != null)
            {
                string username="";
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user != null)
                {
                    username = user.UserName;
                }
                string order = "";
                switch (orderindex)
                {
                    case 1: order = "Random"; break;
                    case 2: order = "Newest"; break;
                    case 3: order = "Popularity"; break;
                    case 4: order = "Hotsale"; break;
                    case 5: order = "Rating"; break;
                    default:
                        break;
                }
                
                int recordcount = 0;
                IList<Model.ServiceInfo> servicelist = BLL.ServiceInfoBLL.Instance.GetPagedServiceInfo(category,key,order,username, hasvideo, mode, pageindex, pagesize, ref recordcount);

                //将flv路径改为mp4路径
                foreach (var service in servicelist)
                {
                    foreach (var att in service.AttachList)
                    {
                        if (!string.IsNullOrEmpty(att.VideoPath))
                        { 
                            string path=att.VideoPath;
                            string ext=path.Substring(path.LastIndexOf('.'), 4);
                            if (ext==Common.PageBase.GetEnumDesc(typeof(Common.FormatConverter.VideoType),Common.FormatConverter.VideoType.FLV))
                            {
                                path = path.Replace(ext, Common.PageBase.GetEnumDesc(typeof(Common.FormatConverter.VideoType), Common.FormatConverter.VideoType.MP4));
                                if (File.Exists(Server.MapPath(path)))
                                {
                                    att.VideoPath = path;
                                }
                            }
                        }
                    }
                }


                //string json=BLL.ServiceInfoBLL.Instance.GetPagedJson(category,order,username,hasvideo,mode,pageindex,pagesize,ref recordcount);
                Hashtable ht = new Hashtable();
                ht.Add("RecordCount", recordcount);
                ht.Add("ServiceList",servicelist);
                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string category, int orderindex, string userid, string key, int mode, int hasvideo, int pageindex, int pagesize)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = public_key + current_day + category + userid + key + (orderindex + mode + hasvideo + pageindex + pagesize).ToString();
                    return secretkey == finalkey;   
                }
            }
            else
            {
                return true;
            }
        }
    }
}